package com.y.ari.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.y.ari.common.AriException;
import com.y.ari.common.RestResponse;
import com.y.ari.common.UserHolder;
import com.y.ari.dto.ContactsDTO;
import com.y.ari.entity.Contacts;
import com.y.ari.entity.DefaultContact;
import com.y.ari.mapper.ContactsMapper;
import com.y.ari.mapper.DefaultContactMapper;
import com.y.ari.service.ContactsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 常用联系人信息 服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j
@Service
public class ContactsServiceImpl extends ServiceImpl<ContactsMapper, Contacts> implements ContactsService {

    @Autowired
    ContactsMapper contactsMapper;

    @Autowired
    DefaultContactMapper defaultContactMapper;

    @Override
    public RestResponse selC() {
        Long id = UserHolder.getUser().getId();
        List<ContactsDTO> contactsDTOS = contactsMapper.selC(id);
        return RestResponse.success(contactsDTOS, "");
    }

    @Transactional
    @Override
    public RestResponse saveC(ContactsDTO contactsDTO) {
        if (contactsDTO.getDefaultContact()) {
            LambdaQueryWrapper<DefaultContact> queryWrapper = new LambdaQueryWrapper<>();
            Integer count = defaultContactMapper.selectCount(queryWrapper);
            if (count == 0) {
                DefaultContact defaultContact = new DefaultContact();
                defaultContact.setDefaultContact(contactsDTO.getDefaultContact());
                defaultContact.setTopContactsId(null);
                int idc = defaultContactMapper.insert(defaultContact);
                if (idc == 0) {
                    AriException.cast("默认联系人设置失败");
                }
            }
        }

        Contacts contacts = new Contacts();
        BeanUtils.copyProperties(contactsDTO, contacts);

        if (contactsDTO.getId() == null) {
            Long id = UserHolder.getUser().getId();
            contacts.setUserId(id);
            int ic = contactsMapper.insert(contacts);
            if (ic == 0) {
                AriException.cast("添加失败");
            }
            setD(contacts.getId(), contactsDTO.getDefaultContact(), "添加");
            return RestResponse.success("添加成功");
        }

        int uc = contactsMapper.updateById(contacts);
        if (uc == 0) {
            AriException.cast("修改失败");
        }
        setD(contactsDTO.getId(), contactsDTO.getDefaultContact(), "修改");
        return RestResponse.success("修改成功");
    }

    public void setD(Long topContactsId, Boolean b, String str) {
        DefaultContact defaultContact = new DefaultContact();
        if (b) {
            defaultContact.setTopContactsId(topContactsId);

            int uDc = defaultContactMapper.update(defaultContact, new LambdaQueryWrapper<>());
            if (uDc == 0) {
                AriException.cast(str + "失败");
            }
            return;
        }
        LambdaQueryWrapper<DefaultContact> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DefaultContact::getTopContactsId, topContactsId);
        defaultContact.setTopContactsId(-1L);
        defaultContactMapper.update(defaultContact, queryWrapper);
    }

    @Transactional
    @Override
    public RestResponse setDc(Long id) {
        LambdaQueryWrapper<DefaultContact> queryWrapper = new LambdaQueryWrapper<>();
        Integer count = defaultContactMapper.selectCount(queryWrapper);
        if (count == 0) {
            DefaultContact defaultContact = new DefaultContact();
            defaultContact.setDefaultContact(true);
            defaultContact.setTopContactsId(id);
            int idc = defaultContactMapper.insert(defaultContact);
            if (idc == 0) {
                AriException.cast("设置失败");
            }
            return RestResponse.success("设置成功");
        }
        setD(id, true, "设置");
        return RestResponse.success("设置成功");
    }

    @Transactional
    @Override
    public RestResponse delBath(List<Long> ids) {
        int i = contactsMapper.deleteBatchIds(ids);
        if (i == 0) {
            AriException.cast("删除失败");
        }
        LambdaQueryWrapper<DefaultContact> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(DefaultContact::getTopContactsId, ids);

        DefaultContact defaultContact = new DefaultContact();
        defaultContact.setTopContactsId(-1L);

        defaultContactMapper.update(defaultContact, queryWrapper);

        return RestResponse.success("删除成功");
    }
}
